<!-- 选项 -->

<template>
  <FormSlot label="选项配置" bind="optionList" noMargin required>
    <div class="IOT-PropForm-MyOption">
      <div class="flexCC alignCenter">
        <div class="flex2"><b>类型</b></div>
        <div class="flex2"><b>内容</b></div>
        <div class="flex1 alignCenter" />
      </div>

      <draggable v-model="one.optionList" class="IOT-PropForm-MyOption-list" :animation="200" :disabled="isShow">
        <FormSlot
          v-for="(item, i) in one.optionList"
          :key="item.id"
          :bind="`child_${item.id}`"
          tip="请填写完整"
          noLabel
          required
        >
          <div class="flexCC">
            <FormInput :one="item" bind="itemName" :max="100" noLabel flex2 @change="toChange(item)" />
            <FormInput :one="item" bind="itemContent" :max="100" noLabel flex2 @change="toChange(item)" />
            <div class="flex1 alignCenter">
              <BtnText v-if="!isShow" icon="el-icon-plus" class="marginRight" @click="addChild(i)" />
              <BtnText
                v-if="!isShow"
                :disabled="one.optionList.length === 1"
                icon="el-icon-minus"
                red
                @click="delChild(i)"
              />
            </div>
          </div>
        </FormSlot>
      </draggable>
    </div>
  </FormSlot>
</template>

<script>
// ============================== 导入依赖 ============================== //

import mixMy from '../../mixMy'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyOption',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [mixMy],

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 子项改变
     */
    toChange(item) {
      const readyStr = item.itemName && item.itemContent ? 'ready' : ''
      this.$set(this.one, `child_${item.id}`, readyStr)
    },

    /**
     * 添加子项
     */
    addChild(i) {
      const { one } = this
      const child = { label: '', value: '' }
      const id = `${Date.now()}`
      this.$set(one, `child_${id}`, '')
      one.optionList.splice(i + 1, 0, { ...child, id })
    },

    /**
     * 删除子项
     */
    delChild(i) {
      this.one.optionList.splice(i, 1)
    },
  },
}
</script>

<style lang="scss">
@import './index.scss';
</style>
